package com.zx.disneyholidays.page;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.zx.disneyholidays.MyApplication;
import com.zx.disneyholidays.R;
import com.zx.disneyholidays.net.BaseResponse;
import com.zx.disneyholidays.net.HttpUtils;
import com.zx.disneyholidays.net.ResponseCallBack;
import com.zx.disneyholidays.page.coupon.list.CouponListActivity;
import com.zx.disneyholidays.utils.StatusBarUtils;

public class MainActivity extends AppCompatActivity {
    ImageView ivBanner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //核心代码.
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        //给状态栏设置颜色。我设置透明。
        window.setStatusBarColor(Color.TRANSPARENT);
        window.setNavigationBarColor(Color.TRANSPARENT);
        setContentView(R.layout.activity_main);

        StatusBarUtils.setTextDark(this, true);
        ivBanner = findViewById(R.id.iv_banner);
        requestHomeData();

        findViewById(R.id.cl_list).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, CouponListActivity.class));
            }
        });

        findViewById(R.id.fl_bottom).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, CouponListActivity.class));
            }
        });
    }


    // 字体大小不跟随系统
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(getConfigurationContext(newBase));
    }

    private static Context getConfigurationContext(Context context) {
        Configuration configuration = context.getResources().getConfiguration();
        configuration.fontScale = 1;
        return context.createConfigurationContext(configuration);
    }


    private void requestHomeData() {
        HttpUtils.instance.httpGet("api/index/top_image", null, new ResponseCallBack() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (data.getCode() != 1) {
                    Glide.with(MainActivity.this)
                            .load(R.mipmap.ic_home_banner)
                            .into(ivBanner);
                    return;
                }


                Glide.with(MainActivity.this)
                        .load(data.getData())
                        .into(ivBanner);

            }

            @Override
            public void onError(int error, String msg) {
                Toast.makeText(MyApplication.applicationContext, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}